package com.lineage.william;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1PcQuest;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import com.lineage.server.timecontroller.server.ServerUseMapTimer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;

public class Npc_ins {
  private static ArrayList<ArrayList<Object>> aData = new ArrayList<>();
  
  private static boolean NO_GET_DATA = false;
  
  public static final String TOKEN = ",";
  
  public static boolean forNpcQuest(String s, L1PcInstance pc, L1NpcInstance npc, int npcid, int oid) {
    ArrayList aTempData = null;
    if (!NO_GET_DATA) {
      NO_GET_DATA = true;
      getData();
    } 
    L1PcQuest quest = pc.getQuest();
    int i = 0;
    while (i < aData.size()) {
      aTempData = (ArrayList)aData.get(i);
      if (aTempData.get(0) != null && ((Integer)aTempData.get(0)).intValue() == npcid && (
        (String)aTempData.get(1)).equals(s) && ((
        (int[])aTempData.get(11) != null && (int[])aTempData.get(12) != null && 
        !pc.getInventory().checkItem((int[])aTempData.get(11), (int[])aTempData.get(12))) || (
        (int[])aTempData.get(11) == null && (int[])aTempData.get(12) == null)) && ((
        (int[])aTempData.get(9) != null && (int[])aTempData.get(10) != null && 
        pc.getInventory().checkItem((int[])aTempData.get(9), (int[])aTempData.get(10))) || (
        (int[])aTempData.get(9) == null && (int[])aTempData.get(10) == null))) {
        if (aTempData.get(0) != null && ((Integer)aTempData.get(0)).intValue() == 220179 && (
          (String)aTempData.get(1)).equals(s) && !pc.getDolls().isEmpty()) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage("请先解除魔法娃娃。"));
          return false;
        } 
        int week = ((Integer)aTempData.get(29)).intValue();
        int starttime = ((Integer)aTempData.get(30)).intValue();
        int unitytime = ((Integer)aTempData.get(31)).intValue();
        Calendar date = Calendar.getInstance();
        int nowWeek = date.get(7) - 1;
        int nowHour = date.get(11);
        if (week > 0 && starttime < 0 && unitytime < 0) {
          if (week != nowWeek) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:星期【" + week + " 】"));
            return false;
          } 
        } else if (week > 0 && starttime >= 0 && unitytime >= 0) {
          if (week != nowWeek || nowHour < starttime || nowHour >= unitytime) {
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, 
                  "使用时间限制:星期【 " + week + " 】时间为【 " + starttime + " 】点，至【 " + unitytime + "】点"));
            return false;
          } 
        } else if (week < 0 && starttime >= 0 && unitytime >= 0 && (
          nowHour < starttime || nowHour >= unitytime)) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "使用时间限制:时间为【 " + starttime + " 】点，至【 " + unitytime + "】点"));
          return false;
        } 
        if (((Integer)aTempData.get(3)).intValue() != 0) {
          byte class_id = 0;
          String msg = "";
          if (pc.isCrown()) {
            class_id = 1;
          } else if (pc.isKnight()) {
            class_id = 2;
          } else if (pc.isWizard()) {
            class_id = 3;
          } else if (pc.isElf()) {
            class_id = 4;
          } else if (pc.isDarkelf()) {
            class_id = 5;
          } else if (pc.isDragonKnight()) {
            class_id = 6;
          } else if (pc.isIllusionist()) {
            class_id = 7;
          } 
          switch (((Integer)aTempData.get(3)).intValue()) {
            case 1:
              msg = "王族";
              break;
            case 2:
              msg = "骑士";
              break;
            case 3:
              msg = "法师";
              break;
            case 4:
              msg = "妖精";
              break;
            case 5:
              msg = "黑暗妖精";
              break;
            case 6:
              msg = "龙骑士";
              break;
            case 7:
              msg = "幻术士";
              break;
          } 
          if (((Integer)aTempData.get(3)).intValue() != class_id) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("你的职业无法使用" + msg + "的专属道具。"));
            return false;
          } 
        } 
        if (((Integer)aTempData.get(0)).intValue() == npcid && ((String)aTempData.get(1)).equals(s) && 
          pc.getQuest().get_step(((Integer)aTempData.get(5)).intValue()) == 255) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("您已经执行过上限1次"));
          return false;
        } 
        if (pc.getQuest().get_step(9050) == 1 || pc.getQuest().get_step(9051) == 1 || 
          pc.getQuest().get_step(9052) == 1 || pc.getQuest().get_step(9053) == 1 || 
          pc.getQuest().get_step(9054) == 1 || pc.getQuest().get_step(9055) == 1 || 
          pc.getQuest().get_step(9056) == 1 || pc.getQuest().get_step(9057) == 1 || 
          pc.getQuest().get_step(9058) == 1 || pc.getQuest().get_step(9059) == 1 || 
          pc.getQuest().get_step(9060) == 1 || pc.getQuest().get_step(9061) == 1 || 
          pc.getQuest().get_step(9062) == 1 || pc.getQuest().get_step(9063) == 1 || 
          pc.getQuest().get_step(9064) == 1 || pc.getQuest().get_step(9065) == 1 || 
          pc.getQuest().get_step(9066) == 1 || pc.getQuest().get_step(9067) == 1 || 
          pc.getQuest().get_step(9068) == 1 || pc.getQuest().get_step(9069) == 1) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("您目前有地图任务进行中,请先完成在接下个任务。。"));
          return false;
        } 
        if (((Integer)aTempData.get(35)).intValue() == 0 && ((Integer)aTempData.get(6)).intValue() != 255 && (
          pc.getQuest().get_step(9001) == 1 || pc.getQuest().get_step(9002) == 1 || 
          pc.getQuest().get_step(9003) == 1 || pc.getQuest().get_step(9004) == 1 || 
          pc.getQuest().get_step(9005) == 1 || pc.getQuest().get_step(9006) == 1 || 
          pc.getQuest().get_step(9007) == 1 || pc.getQuest().get_step(9008) == 1 || 
          pc.getQuest().get_step(9009) == 1 || pc.getQuest().get_step(9010) == 1 || 
          pc.getQuest().get_step(9011) == 1 || pc.getQuest().get_step(9012) == 1 || 
          pc.getQuest().get_step(9013) == 1 || pc.getQuest().get_step(9014) == 1 || 
          pc.getQuest().get_step(9015) == 1 || pc.getQuest().get_step(9016) == 1 || 
          pc.getQuest().get_step(9017) == 1 || pc.getQuest().get_step(9018) == 1 || 
          pc.getQuest().get_step(9019) == 1 || pc.getQuest().get_step(9020) == 1 || 
          pc.getQuest().get_step(9021) == 1 || pc.getQuest().get_step(9022) == 1 || 
          pc.getQuest().get_step(9023) == 1 || pc.getQuest().get_step(9024) == 1 || 
          pc.getQuest().get_step(9025) == 1 || pc.getQuest().get_step(9026) == 1 || 
          pc.getQuest().get_step(9027) == 1 || pc.getQuest().get_step(9028) == 1 || 
          pc.getQuest().get_step(9029) == 1 || pc.getQuest().get_step(9030) == 1)) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("您目前有每日任务进行中,请先完成在接下个任务。。"));
          return false;
        } 
        if (((Integer)aTempData.get(6)).intValue() != 0 && pc.getQuest().get_step((
            (Integer)aTempData.get(18)).intValue()) == ((Integer)aTempData.get(6)).intValue()) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("您已经执行过上限" + ((Integer)aTempData.get(6)).intValue() + "次。"));
          return false;
        } 
        if (pc.getMapId() == 8544 || pc.getMapId() == 99 || (pc.getMapId() >= 8700 && pc.getMapId() <= 8726)) {
          pc.sendPackets((ServerBasePacket)new S_ServerMessage(166, "\\fU此地区无法进行!!"));
          return false;
        } 
        if (pc.getLevel() >= ((Integer)aTempData.get(2)).intValue() && 
          pc.getLevel() <= ((Integer)aTempData.get(32)).intValue()) {
          boolean isGet = false;
          boolean isCreate = true;
          if ((int[])aTempData.get(13) != null && (int[])aTempData.get(14) != null) {
            int[] materials = (int[])aTempData.get(13);
            int[] counts = (int[])aTempData.get(14);
            int j = 0;
            while (j < materials.length) {
              if (!pc.getInventory().checkItem(materials[j], counts[j])) {
                L1Item temp = ItemTable.get().getTemplate(materials[j]);
                pc.sendPackets((ServerBasePacket)new S_ServerMessage(337, String.valueOf(String.valueOf(temp.getName())) + "(" + (
                      counts[j] - pc.getInventory().countItems(temp.getItemId())) + ")"));
                isCreate = false;
              } 
              j++;
            } 
            if (!isCreate) {
              if ((String[])aTempData.get(23) != null) {
                pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(oid, (String)aTempData.get(22), 
                      (String[])aTempData.get(23)));
              } else if (aTempData.get(22) != null) {
                pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(oid, (String)aTempData.get(22)));
              } 
              return true;
            } 
            if (isCreate && ((Integer)aTempData.get(15)).intValue() == 0 && (
              (Integer)aTempData.get(34)).intValue() == 1) {
              int k = 0;
              while (k < materials.length) {
                pc.getInventory().consumeItem(materials[k], counts[k]);
                k++;
              } 
            } 
          } 
          if (((Integer)aTempData.get(15)).intValue() == 0 && (int[])aTempData.get(16) != null && 
            (int[])aTempData.get(17) != null) {
            int[] giveMaterials = (int[])aTempData.get(16);
            int[] giveCounts = (int[])aTempData.get(17);
            int l = 0;
            while (l < giveMaterials.length) {
              L1ItemInstance item = ItemTable.get().createItem(giveMaterials[l]);
              if (item.getBless() >= 4) {
                pc.sendPackets((ServerBasePacket)new S_SystemMessage("该装备受到封印卷轴的影响。"));
                return false;
              } 
              if (item.isStackable()) {
                item.setCount(giveCounts[l]);
              } else {
                item.setCount(1L);
              } 
              if (item != null) {
                isGet = true;
                if (pc.getInventory().checkAddItem(item, giveCounts[l]) != 0)
                  return false; 
                pc.getInventory().storeItem(item);
                pc.sendPackets(
                    (ServerBasePacket)new S_ServerMessage(143, npc.getNpcTemplate().get_name(), item.getLogName()));
              } 
              l++;
            } 
          } 
          pc.getQuest().set_step(((Integer)aTempData.get(18)).intValue(), (
              (Integer)aTempData.get(19)).intValue());
          if ((String)aTempData.get(28) != null)
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("\\fY" + (String)aTempData.get(28))); 
          if (((Integer)aTempData.get(27)).intValue() != 0 && (isGet || isCreate)) {
            pc.get_other().set_usemap(-1);
            pc.get_other().set_usemapTime(0);
            L1Teleport.teleport(pc, ((Integer)aTempData.get(24)).intValue(), (
                (Integer)aTempData.get(25)).intValue(), 
                (short)((Integer)aTempData.get(26)).intValue(), 5, true);
            pc.get_other().set_usemap(((Integer)aTempData.get(26)).intValue());
            ServerUseMapTimer.put(pc, ((Integer)aTempData.get(27)).intValue());
            pc.sendPackets((ServerBasePacket)new S_ServerMessage("时间限制:" + ((Integer)aTempData.get(27)).intValue() + "秒"));
            return true;
          } 
          if (((Integer)aTempData.get(33)).intValue() > 0)
            pc.addExp(((Integer)aTempData.get(33)).intValue()); 
          if ((String[])aTempData.get(21) != null && (isGet || isCreate)) {
            pc.sendPackets(
                (ServerBasePacket)new S_NPCTalkReturn(oid, (String)aTempData.get(20), (String[])aTempData.get(21)));
            return true;
          } 
          if (aTempData.get(20) != null && (isGet || isCreate)) {
            pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(oid, (String)aTempData.get(20)));
            return true;
          } 
          pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(oid, ""));
          return true;
        } 
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("等级最低需求:" + ((Integer)aTempData.get(2)).intValue()));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("等级最高需求:" + ((Integer)aTempData.get(32)).intValue()));
      } 
      i++;
    } 
    return false;
  }
  
  private static void getData() {
    Connection con = null;
    try {
      con = DatabaseFactory.get().getConnection();
      Statement stat = con.createStatement();
      ResultSet rset = stat.executeQuery("SELECT * FROM w_狩猎怪物任务_系统");
      ArrayList<Object> aReturn = null;
      String sTemp = null;
      if (rset != null)
        while (rset.next()) {
          aReturn = new ArrayList();
          aReturn.add(0, new Integer(rset.getInt("npcid")));
          sTemp = rset.getString("action");
          aReturn.add(1, sTemp);
          aReturn.add(2, Integer.valueOf(rset.getInt("checkLevel_min")));
          aReturn.add(3, Integer.valueOf(rset.getInt("checkClass")));
          aReturn.add(4, new Integer(rset.getInt("checkPoly")));
          aReturn.add(5, new Integer(rset.getInt("check_QuestId")));
          aReturn.add(6, new Integer(rset.getInt("check_Quest_set")));
          aReturn.add(7, new Integer(rset.getInt("notHaveQuestId")));
          aReturn.add(8, new Integer(rset.getInt("notHaveQuestOrder")));
          if (rset.getString("checkItem") != null && !rset.getString("checkItem").equals("") && 
            !rset.getString("checkItem").equals("0")) {
            aReturn.add(9, getArray(rset.getString("checkItem"), ",", 1));
          } else {
            aReturn.add(9, null);
          } 
          if (rset.getString("checkItemCount") != null && !rset.getString("checkItemCount").equals("") && 
            !rset.getString("checkItemCount").equals("0")) {
            aReturn.add(10, getArray(rset.getString("checkItemCount"), ",", 1));
          } else {
            aReturn.add(10, null);
          } 
          if (rset.getString("notHaveItem") != null && !rset.getString("notHaveItem").equals("") && 
            !rset.getString("notHaveItem").equals("0")) {
            aReturn.add(11, getArray(rset.getString("notHaveItem"), ",", 1));
          } else {
            aReturn.add(11, null);
          } 
          if (rset.getString("notHaveItemCount") != null && !rset.getString("notHaveItemCount").equals("") && 
            !rset.getString("notHaveItemCount").equals("0")) {
            aReturn.add(12, getArray(rset.getString("notHaveItemCount"), ",", 1));
          } else {
            aReturn.add(12, null);
          } 
          if (rset.getString("material") != null && !rset.getString("material").equals("") && 
            !rset.getString("material").equals("0")) {
            aReturn.add(13, getArray(rset.getString("material"), ",", 1));
          } else {
            aReturn.add(13, null);
          } 
          if (rset.getString("materialCount") != null && !rset.getString("materialCount").equals("") && 
            !rset.getString("materialCount").equals("0")) {
            aReturn.add(14, getArray(rset.getString("materialCount"), ",", 1));
          } else {
            aReturn.add(14, null);
          } 
          aReturn.add(15, new Integer(rset.getInt("justCheckMaterial")));
          if (rset.getString("GiveItem") != null && !rset.getString("GiveItem").equals("") && 
            !rset.getString("GiveItem").equals("0")) {
            aReturn.add(16, getArray(rset.getString("GiveItem"), ",", 1));
          } else {
            aReturn.add(16, null);
          } 
          if (rset.getString("GiveItemCount") != null && !rset.getString("GiveItemCount").equals("") && 
            !rset.getString("GiveItemCount").equals("0")) {
            aReturn.add(17, getArray(rset.getString("GiveItemCount"), ",", 1));
          } else {
            aReturn.add(17, null);
          } 
          aReturn.add(18, new Integer(rset.getInt("save_QuestId")));
          aReturn.add(19, new Integer(rset.getInt("save_Questset")));
          if (rset.getString("ShowHtml") != null && !rset.getString("ShowHtml").equals("")) {
            aReturn.add(20, new String(rset.getString("ShowHtml")));
          } else {
            aReturn.add(20, null);
          } 
          if (rset.getString("ShowHtmlData") != null && !rset.getString("ShowHtmlData").equals("")) {
            aReturn.add(21, getArray(rset.getString("ShowHtmlData"), ",", 2));
          } else {
            aReturn.add(21, null);
          } 
          if (rset.getString("ShowNotHaveHtml") != null && !rset.getString("ShowNotHaveHtml").equals("")) {
            aReturn.add(22, new String(rset.getString("ShowNotHaveHtml")));
          } else {
            aReturn.add(22, null);
          } 
          if (rset.getString("ShowNotHaveHtmlData") != null && 
            !rset.getString("ShowNotHaveHtmlData").equals("")) {
            aReturn.add(23, getArray(rset.getString("ShowNotHaveHtmlData"), ",", 2));
          } else {
            aReturn.add(23, null);
          } 
          aReturn.add(24, new Integer(rset.getInt("tele_x")));
          aReturn.add(25, new Integer(rset.getInt("tele_y")));
          aReturn.add(26, new Integer(rset.getInt("tele_mapid")));
          aReturn.add(27, new Integer(rset.getInt("time")));
          aReturn.add(28, rset.getString("pc_message"));
          aReturn.add(29, new Integer(rset.getInt("week")));
          aReturn.add(30, new Integer(rset.getInt("startime")));
          aReturn.add(31, new Integer(rset.getInt("endtime")));
          aReturn.add(32, Integer.valueOf(rset.getInt("checkLevel_max")));
          aReturn.add(33, Integer.valueOf(rset.getInt("addexp")));
          aReturn.add(34, Integer.valueOf(rset.getInt("删除触发道具")));
          aReturn.add(35, Integer.valueOf(rset.getInt("questend")));
          aData.add(aReturn);
        }  
      if (con != null && !con.isClosed())
        con.close(); 
    } catch (Exception exception) {}
  }
  
  private static Object getArray(String s, String sToken, int iType) {
    StringTokenizer st = new StringTokenizer(s, sToken);
    int iSize = st.countTokens();
    String sTemp = null;
    if (iType == 1) {
      int[] iReturn = new int[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        iReturn[i] = Integer.parseInt(sTemp);
        i++;
      } 
      return iReturn;
    } 
    if (iType == 2) {
      String[] sReturn = new String[iSize];
      int i = 0;
      while (i < iSize) {
        sTemp = st.nextToken();
        sReturn[i] = sTemp;
        i++;
      } 
      return sReturn;
    } 
    if (iType == 3) {
      String sReturn2 = null;
      int i = 0;
      while (i < iSize) {
        sTemp = sReturn2 = st.nextToken();
        i++;
      } 
      return sReturn2;
    } 
    return null;
  }
}
